<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org" xmlns:shiro="http://www.pollix.at/thymeleaf/shiro">
<head lang="en" th:replace="include/common/common :: commonHeader">
    <footer th:include="include/common/common :: layUiResource"></footer>
    <footer th:include="include/common/common :: blueCSSResource"></footer>
    <footer th:include="include/common/common :: zTreeCSSSource"></footer>
    <footer th:include="include/common/common :: zTreeJSSource"></footer>

    <script>
        layui.config({
            base: '/staticResource/layuiadmin/' //静态资源所在路径
        }).extend({
            index: 'lib/index' //主入口模块
        }).use(['index' , 'table'] , function () {
            var table = layui.table
                ,admin = layui.admin
                ,element = layui.element;

            let entityName = 'config'
                ,isSystem = [[${isSystem}]]
                ,pageSize = [[${pageSize}]]
                ,tableId = entityName + "-table-operate"
                ,cols = [
                    [
                        {type:'checkbox', fixed: 'left'}
                        ,{field:'id', title: 'ID', sort: true, hide:true, fixed: 'left'}
                        ,{field:'configName', sort:true , title: '名称'}
                        ,{field:'configKey', sort:true , title: '参数键'}
                        ,{field:'configValue', sort:true , title: '参数值'}
                        ,{title:'操作' , align:'center', fixed: 'right', toolbar: '#test-table-operate-barDemo'}
                    ]
                ];

              //定义indexTree的设置
            let _entityName = 'organization'
                ,treeSetting = {
                // 回调定义
                callback:{
                    beforeClick: zTreeBeforeClick ,
                    // 鼠标单击 回调
                    onClick:zTreeOnClick
                }
            }
                ,_treeId = _entityName + 'Tree' , _searchBtnId = _entityName + 'SearchBtn'
                ,_searchInputId = _entityName + 'SearchInput' , _treeDataUrl = "/a/organization/treeData"
                //定义menuTree的参数
                ,userTreeOptions = {
                treeId:_treeId
                ,searchBtnId: _searchBtnId
                ,searchInputId:_searchInputId
                ,setting:treeSetting
                ,isExpandAll:true
                ,url:_treeDataUrl
            };

            //声明tree鼠标单击回调事件的动作，layui 弹层
            let active = {
                btn:function(jsonData){
                    $("#searchOrgCode").val(jsonData.id);
                    let searchBtn = '#' + entityName + 'SearchBtn';
                    $.fn.blueJs.tools.active['layTable'].call(this , table , searchBtn , pageSize , cols , tableId);
                }
            };

            //加载tree
            function loadTree(){
                $.fn.zTree.destroy(userTreeOptions.treeId);
                $.fn.blueJs.zTreeTool.loadTree(userTreeOptions);
            };

            /**
             * treeNode鼠标单击 回调
             * @param event
             * @param treeId
             * @param treeNode
             */
            function zTreeOnClick(event, treeId, treeNode) {
                active['btn'] && active['btn'].call(this , treeNode);
            };

            function zTreeBeforeClick(treeId, treeNode, clickFlag) {
/*                let flag = treeNode.isTreeLeaf || userTreeOptions.canSelectParent;
                // 非末级节点时，如果不允许选择，则展开/收起节点
                if (!flag){
                    let treeObj = $.fn.blueJs.zTreeTool.getTreeObj(userTreeOptions.treeId);
                    treeObj.expandNode(treeNode);
                    // alert("非末级节点，不允许选择！");
                }
                return flag;*/
            }

            //点击事件监听
            $(document).on('click' , function (event) {
                let targetId = event.target.id;
                if (targetId == _searchBtnId || targetId == _searchInputId){
                    fuzzSearch(_treeId,'#' + _searchInputId,null,true); //初始化模糊搜索方法
                }
                if (targetId == entityName + 'SearchBtn'){
                    let searchBtn = '#' + targetId;
                    $.fn.blueJs.tools.active['layTable'].call(this , table , searchBtn , pageSize , cols , tableId);
                }
            });

            $(document).ready(function () {
                $.fn.blueJs.tools.showIndex(isSystem , entityName);
                loadTree();
                let searchBtn = '#' + entityName + 'SearchBtn';
                $.fn.blueJs.tools.active['layTable'].call(this , table , searchBtn , pageSize , cols , tableId);
            });

            //监听工具条

            //todo:以下测试，未完成
            //监听表格复选框选择
            table.on('checkbox(' + entityName + '-table-operate)', function(obj){
                console.log(obj)
            });

            table.on('tool(' + entityName + '-table-operate)', function(obj){
                var data = obj.data;
                if(obj.event === 'detail'){
                    layer.alert('查看：<br>'+ JSON.stringify(data))
                    // layer.msg('ID：'+ data.id + ' 的查看操作');
                } else if(obj.event === 'del'){
                    layer.confirm('真的删除行么', function(index){
                        obj.del();
                        layer.close(index);
                    });
                } else if(obj.event === 'edit'){
                    let i = "/a/" + entityName + "/form?id=" + data.id , t = "参数信息";
                    $.fn.blueJs.tools.active['layerOpen'].call(this , i , t);
                }
            });

            let tableActive = {
                getCheckData: function(){ //获取选中数据
                    var checkStatus = table.checkStatus('test-table-operate')
                        ,data = checkStatus.data;
                    layer.alert(JSON.stringify(data));
                }
                ,getCheckLength: function(){ //获取选中数目
                    var checkStatus = table.checkStatus('test-table-operate')
                        ,data = checkStatus.data;
                    layer.msg('选中了：'+ data.length + ' 个');
                }
                ,add: function(){
                    layui.router();
                    let i = "/a/" + entityName + "/form?isNewRecord=true" , t = "参数信息";
                    $.fn.blueJs.tools.active['layerOpen'].call(this , i , t);
                }
            };

            $('.test-table-operate-btn .layui-btn').on('click', function(){
                let type = $(this).data('type');
                tableActive[type] ? tableActive[type].call(this) : '';
            });
        });
    </script>
</head>
<body>
<div class="layui-fluid">
    <div class="layui-row layui-col-space15">
        <!--        左侧tree-->
        <div class="layui-col-md2" id="config-tree-index" style="display: none">
            <div class="layui-card">
                <div class="layui-card-header">
                    组织信息
                </div>
                <div class="layui-card-body">
                    <div class="layui-btn-container">
                        <div class="layui-btn-group">
<!--                            <button class="layui-btn" id="organizationAdd">新增</button>-->
                            <button class="layui-btn" id="organizationRefresh">刷新</button>
                        </div>
                    </div>
                </div>
            </div>
            <div class="layui-card">
                <div class="layui-card-header"></div>
                <div class="layui-card-body">
                    <div class="form-group treeSearchInput" id="search" style="display: block">
                        <!--                <label for="searchInput">关键字：</label>-->
                        <input type="text" id="organizationSearchInput" maxlength="50" class="empty"/>
                        <button class="btn" id="organizationSearchBtn" >搜索</button>
                    </div>
                    <div id="organizationTree" class="ztree"></div>
                </div>
            </div>
        </div>
        <!--        右侧表单-->
        <div class="layui-col-md10" id="config-panel">
            <div class="layui-card">
                <div class="layui-card-header">系统参数</div>
                <div class="layui-card-body">
                    <div class="layui-row layui-col-space10 layui-form-item">
                        <!--                        todo:id = entity + search + form-->
                        <form class="layui-form" id="configSearchForm" th:object="${config}" method="post" action="/a/config/findPage">
                            <input type="text" name="orgCode" id="searchOrgCode" placeholder="组织标识" style="display: none" autocomplete="off" class="layui-input">
                            <div class="layui-form-item">
                                <div class="layui-inline">
                                    <label class="layui-form-label">名称：</label>
                                    <div class="layui-input-block">
                                        <input type="text" name="configName" id="searchConfigName" placeholder="名称" autocomplete="off" class="layui-input">
                                    </div>
                                </div>

                                <div class="layui-inline">
                                    <label class="layui-form-label">参数键：</label>
                                    <div class="layui-input-block">
                                        <input type="text" name="configKey" id="searchConfigKey" placeholder="参数键" autocomplete="off" class="layui-input">
                                    </div>
                                </div>

                                <div class="layui-inline">
                                    <label class="layui-form-label">参数值：</label>
                                    <div class="layui-input-block">
                                        <input type="text" name="configValue" id="searchConfigValue" placeholder="参数值" autocomplete="off" class="layui-input">
                                    </div>
                                </div>
                            </div>

                            <div class="layui-form-item">
                                <!--                        todo:blue-form-id = entity + search + form-->
                                <button type="button" class="layui-btn" id="configSearchBtn" onsubmit="return false" blue-form-id="configSearchForm">搜索</button>
                                <button type="reset" class="layui-btn layui-btn-primary">重置</button>
                            </div>
                        </form>
                    </div>

                </div>
            </div>
            <div class="layui-card">
                <div class="layui-card-body" >
                    <div class="layui-btn-group test-table-operate-btn" id="tableBtn" style="margin-bottom: 10px;">
                        <button class="layui-btn layui-btn-sm" data-type="add">新增</button>
                        <button class="layui-btn layui-btn-sm" data-type="getCheckData">获取选中行数据</button>
                        <button class="layui-btn layui-btn-sm" data-type="getCheckLength">获取选中数目</button>
                    </div>
                    <table class="layui-hide" id="config-table-operate" lay-filter="config-table-operate"></table>

                    <script type="text/html" id="test-table-operate-barDemo">
                        <a class="layui-btn layui-btn-primary layui-btn-xs" lay-event="detail">查看</a>
                        <a class="layui-btn layui-btn-xs" lay-event="edit">编辑</a>
                        <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
                    </script>
                </div>

            </div>
        </div>
    </div>
</div>
</body>
</html>